#!/usr/local/bin/python

import os
import sys
import gzip
import time

if len(sys.argv) >= 3:
    x_days = float(sys.argv[1])
    _dir = sys.argv[2]
else:
    while True:
        try:
            x_days = float(raw_input("Please enter the number of days: "))
            break
        except ValueError:
            print "Oops!  That was no valid number.  Try again..."
    _dir = raw_input("Please enter the file path: ")


def file_compression(dir_path):
    dir_files = os.listdir(dir_path)
    for element in dir_files:
        if os.path.isfile(dir_path + "/" + element):
            #print element
            #print time.time() - (x_days * 24 * 60 * 60)
            #print os.stat(dir_path + "/" + element).st_atime
            if ( os.stat(dir_path + "/" + element).st_atime < (time.time() - (x_days * 24 * 60 * 60))):
		f_in = open(dir_path + "/" + element, 'rb')
                f_out = gzip.open("/shots/spi/home/data/outdated/"+element+'.gz', 'wb')
                f_out.writelines(f_in)
                f_out.close()
                f_in.close()
                os.remove(dir_path + "/" + element)
            #print time.ctime(os.stat(dir_path+"/"+element).st_atime)

            elif os.path.isdir(dir_path + "/" + element):
                file_compression(dir_path + "/" + element)
            else:
                continue


file_compression(_dir)
